/*数据处理的方法*/

function computedCustomBenefit(category) {
  //自定义
  let arrayData = [];
  if (category.customBenefit) {
    let customBenefit = JSON.parse(category.customBenefit);
    for (let i = 0; i < customBenefit.length; i++) {
      arrayData.push(`${customBenefit[i].value}`)
    }
    return arrayData
  }
}

//优惠券显示处理(couponDetail)（有效时间处）（日期）
//flag为true,旧逻辑，flag为false新逻辑
function dillCouponDay(listArray, flag = true, availableTime = null) {
  let returnArray = [];
  if (flag) {
    for (let i = 0; i < listArray.length; i++) {
      let listArr = listArray[i].availablePeriod.split(",")
      let listCell = listArray[i]
      let returnString = dillCouponDataItem(listArr);
      let timeString = `${listCell.startHour > 9 ? listCell.startHour : "0" + listCell.startHour}:${listCell.startMinute > 9 ? listCell.startMinute : "0" + listCell.startMinute} -  ${listCell.endHour > 9 ? listCell.endHour : "0" + listCell.endHour}:${listCell.endMinute > 9 ? listCell.endMinute : "0" + listCell.endMinute}`;
      returnArray.push({
        "returnString": returnString,
        "timeString": timeString,
      })
    }
    return returnArray;
  } else {
    console.log("aaa");
    let returnArray = {};
    let listArr = availableTime.availablePeriod.split(",")
    let returnString = dillCouponDataItem(listArr, false);
    let str = mToStrArray(availableTime);
    returnArray = {
      "returnString": returnString,
      "timeString": str,
    }
    console.log('str', str)
    console.log('returnArray', returnArray)
    return returnArray;
  }

}

function dillCouponDataItem(arr, flag = true) {
  let dillArrString;
  if (flag) { dillArrString = '可用时段：'; }
  else { dillArrString = ''; }

  let newArr = [], i = 0;
  newArr[i] = [arr[0]];
  arr.reduce(function (prev, cur) {
    cur - prev === 1 ? newArr[i].push(cur) : newArr[++i] = [cur];
    return cur;
  });
  for (let i = 0; i < newArr.length; i++) {
    let arrItem = newArr[i];
    if (arrItem.length > 1) {
      dillArrString += `${arrItem[0]}-${arrItem[arrItem.length - 1]}号、`
    } else {
      dillArrString += `${arrItem[0]}号、`
    }
  }
  return dillArrString.slice(0, -1);
}

/*周几*/
//flag为true,用旧逻辑，false用新逻辑
function dillCouponWeek(listArray = [], flag = true, availableTime = null) {
  let returnString = '可用时段：';
  let returnArray = [];
  if (flag) {
    for (let i = 0; i < listArray.length; i++) {
      let listCell = listArray[i]
      if (listCell.availablePeriod == '1,2,3,4,5,6,7') {
        returnString = '周一到周日'
      } else if (listCell.availablePeriod == '1,2,3,4,5') {
        returnString = '周一到周五'
      } else if (listCell.availablePeriod == '1,2,3,4,5,6') {
        returnString = '周一到周六'
      } else {
        returnString = dillCouponWeekItem(listCell.availablePeriod);
      }
      let timeString =
        `${listCell.startHour > 9 ? listCell.startHour : "0" + listCell.startHour}:${listCell.startMinute > 9 ? listCell.startMinute : "0" + listCell.startMinute} 
        -  ${listCell.endHour > 9 ? listCell.endHour : "0" + listCell.endHour}:${listCell.endMinute > 9 ? listCell.endMinute : "0" + listCell.endMinute}`;
      returnArray.push({
        "returnString": returnString,
        "timeString": timeString,
      })
    }
    return returnArray;
  } else {
    //新逻辑
    let returnArray = {};
    let timeString = [];
    if (availableTime.availablePeriod == '1,2,3,4,5,6,7') {
      returnString = '周一到周日'
    } else if (availableTime.availablePeriod == '1,2,3,4,5') {
      returnString = '周一到周五'
    } else if (availableTime.availablePeriod == '1,2,3,4,5,6') {
      returnString = '周一到周六'
    } else {
      returnString = dillCouponWeekItem(availableTime.availablePeriod, true);
    }
    let str = mToStrArray(availableTime)
    returnArray = {
      "returnString": returnString,
      "timeString": str,
    };
    return returnArray;
  }
}

//新逻辑组装时间段
function mToStrArray(availableTime) {
  let timeString = []
  if (availableTime.startMinute1 != null && availableTime.endMinute1 != null) {
    let str = mToStr(availableTime.startMinute1) + " ~ " + mToStr(availableTime.endMinute1)
    timeString.push(str)
  }
  if (availableTime.startMinute2 != null && availableTime.endMinute2 != null) {
    let str = mToStr(availableTime.startMinute2) + " ~ " + mToStr(availableTime.endMinute2)
    timeString.push(str)
  }
  if (availableTime.startMinute3 != null && availableTime.endMinute3 != null) {
    let str = mToStr(availableTime.startMinute3) + " ~ " + mToStr(availableTime.endMinute3)
    timeString.push(str)
  };
  console.log('timeString', timeString)
  return timeString
}

function mToStr(m) {
  let h = Math.floor(m / 60);
  m -= h * 60;
  if (h < 9) { h = '0' + h };
  if (m < 9) { m = '0' + m }
  return h + ":" + m
}

//flag true为新逻辑
function dillCouponWeekItem(list, flag) {
  let dillArrString = '';
  if (list) {
    let listArr = list.split(",")
    if (flag) {
      for (let i = 0; i < listArr.length; i++) {
        let listItem = listArr[i];
        if (listItem == 1) {
          dillArrString += '周日'
        } else if (listItem == 2) {
          dillArrString += '周一'
        } else if (listItem == 3) {
          dillArrString += '周二'
        } else if (listItem == 4) {
          dillArrString += '周三'
        } else if (listItem == 5) {
          dillArrString += '周四'
        } else if (listItem == 6) {
          dillArrString += '周五'
        } else if (listItem == 7) {
          dillArrString += '周六'
        }
      }
    } else {
      for (let i = 0; i < listArr.length; i++) {
        let listItem = listArr[i];
        if (listItem == 1) {
          dillArrString += '周一'
        } else if (listItem == 2) {
          dillArrString += '周二'
        } else if (listItem == 3) {
          dillArrString += '周三'
        } else if (listItem == 4) {
          dillArrString += '周四'
        } else if (listItem == 5) {
          dillArrString += '周五'
        } else if (listItem == 6) {
          dillArrString += '周六'
        } else if (listItem == 7) {
          dillArrString += '周日'
        }
      }
    }
    return dillArrString
  }
}
/*
赠送的礼品
* 包括三部分 卡自带 自定义奖励 券赠送
* */
function openCardGift(idAvailable) {
  let category = idAvailable.category
  let openCardCouponAward = idAvailable.openCardCouponAward
  let openCardGiftArr = [];
  //卡自带
  if (category.giftBalance) {
    openCardGiftArr.push({ title: "余额", value: '￥' + category.giftBalance / 100, aFix: "元", iconName: "icon_balance" })
  }
  if (category.giftPoints) {
    openCardGiftArr.push({ title: "积分", value: category.giftPoints, aFix: "个", iconName: "icon_jifen" })
  }
  /*券赠送*/
  if (openCardCouponAward.length > 0) {
    openCardCouponAward.forEach(value => {
      openCardGiftArr.push({ title: value.couponName, value: 'X' + value.amount, aFix: "张", iconName: value.couponType })
    })
  }
  /*自定义*/
  // if(category.customGift){
  //   var customGift = JSON.parse(category.customGift);
  //   customGift.forEach(value=>{
  //     openCardGiftArr.push({title:"开卡奖励",value:value.value,aFix:""})
  //   })
  // }
  return openCardGiftArr
}

export default {
  dillCouponWeek,
  dillCouponDay,
  openCardGift,
  computedCustomBenefit,
  mToStrArray
}
